package jlib.component.core
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	import jlib.utils.RectangleUtil;

	/**
	 * 컴포넌트의 Form 객체.
	 * @author Park Jong Hee
	 */	
	public class Form extends Sprite
	{
		private var m_width:Number = 1;
		private var m_height:Number = 1;

		private var m_source:*;
		private var m_border:Border;
		private var m_background:Background;
		
		/**
		 * Form 객체 생성자. 
		 */		
		public function Form()
		{
			init();
		}

		private function init(): void
		{
			m_border = new Border;
			m_background = new Background(1,1);
			addChild(m_background);
			addChild(m_border);
		}

		/**
		 * Form에 라운드 값을 설정합니다.
		 * @param $round int 값
		 */		
		public function setRound($round:int): void
		{
			m_border.setRound($round);
			m_background.setRound($round);
		}

		/**
		 * Background 를 설정합니다. 
		 * @param $source DisplayObject, uint 
		 */		
		public function setBackground($source:*): void
		{
			m_source = $source;
			if(m_source is DisplayObject) {
				this.width = $source.width;
				this.height = $source.height;
			}
			m_background.setSource($source);
		}

		/**
		 * Form에 Border 값을 설정합니다. 
		 * @param $thickness 두께
		 * @param $color 컬러
		 * @param $type 타입
		 * @param $position 위치.
		 */		
		public function setBorder($thickness:int=0, $color:uint=0x000000, $type:String='solid', $position:String='middle'): void {
			m_border.setThickness($thickness), m_border.setColor($color), m_border.setType($type), m_border.setPosition($position);
		}

		public function update(e:Event=null): void {
			if(!stage) {
				this.addEventListener(Event.ADDED_TO_STAGE, update ) ;
				return ;
			}else this.removeEventListener(Event.ADDED_TO_STAGE, update ) ;
			m_border.width = m_background.width = m_width;
			m_border.height = m_background.height = m_height;
			m_border.update(e);
			m_background.update(e);
		}
		
		override public function get scale9Grid() : Rectangle { return m_background.scale9Grid; }
		override public function set scale9Grid(r:Rectangle) : void {
			m_background.scale9Grid = r;
			update();
		}

		override public function get width(): Number { return m_width; }
		override public function set width(value:Number): void {
			m_width = value;
			update();
		}

		override public function get height(): Number { return m_height; }
		override public function set height(value:Number): void {
			m_height = value;
			update();
		}
		
		override public function getRect(targetCoordinateSpace:DisplayObject):Rectangle {
			return m_background.getRect(targetCoordinateSpace);
		}
		
		override public function getBounds(targetCoordinateSpace:DisplayObject):Rectangle{
			return RectangleUtil.compound(m_border.getBounds(targetCoordinateSpace), m_background.getBounds(targetCoordinateSpace));
		}

	}//c
}